/**
 * 加载数据信息
 */
jQuery(document).ready(function() {
	// 生成的树进行设置
	var setting = {
		view : {
			dblClickExpand : false,
			showLine : true,
			selectedMulti : false,
			expandSpeed : "normal"
		},
		check : {
			enable : true,
			chkStyle : "checkbox"// ,
		// chkboxType : {
		// "Y" : "p",
		// "N" : "s"
		// }
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pIdKey : "pid",
				rootPId : ""
			}
		},
		callback : {
			beforeClick : function(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("tree");
				if (treeNode.isParent) {
					zTree.expandNode(treeNode);
					return false;
				} else {
					demoIframe.attr("src", treeNode.file + ".html");
					return true;
				}
			}
		}
	};
	var jsonData = eval($("#_json_id").val());
	var t = $("#tree");
	t = $.fn.zTree.init(t, setting, jsonData);
	demoIframe = $("#testIframe");
	demoIframe.bind("load", loadReady);
	var zTree = $.fn.zTree.getZTreeObj("tree");
	zTree.selectNode(zTree.getNodeByParam("id", 101));

	// 全部展开节点
	// var treeObj = $.fn.zTree.getZTreeObj("tree");
	// treeObj.expandAll(true);
	// var treeObj = $.fn.zTree.getZTreeObj("tree");
	// var nodes = treeObj.getNodes();
	// if (nodes.length > 0) {
	// treeObj.expandNode(nodes, true, true, true);
	// }
	
	 $("#bind_id").click(function(){
	   	  var form = document.formName;
	   	  form.action=$(this).attr("url");
	   	  _checkedValue();
	   	  form.submit();
	   });
});

function loadReady() {
	var bodyH = demoIframe.contents().find("body").get(0).scrollHeight, htmlH = demoIframe
			.contents().find("html").get(0).scrollHeight, maxH = Math.max(
			bodyH, htmlH), minH = Math.min(bodyH, htmlH), h = demoIframe
			.height() >= maxH ? minH : maxH;
	if (h < 530)
		h = 530;
	demoIframe.height(h);
}

/**
 * 角色获取跳转通用脚本
 * 
 * @param url
 */
function get(url) {
	var form = document.roleForm;
	form.action = url;
	form.submit();
};
/**
 * 保存，需要做校验，所以不通用
 * 
 * @param url
 */
function save(url) {
	var form = document.roleForm;
	form.action = url;
	_checkedValue();
	form.submit();
};
/**
 * 对已经选择的资源进行拼接
 */
function _checkedValue() {
	var treeObj = $.fn.zTree.getZTreeObj("tree");
	var nodes = treeObj.getCheckedNodes(true);
	if (nodes != null) {
		var checkedValue = "";
		var flag = true;
		for (var i = 0; i < nodes.length; i++) {
			if (flag) {
				checkedValue = nodes[i].id;
				flag = false;
			} else {
				checkedValue += ',' + nodes[i].id;
			}
		}
		$("#checked_value_id").val(checkedValue);
	}
}
